package cnm.parser;

import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import cnm.pojo.CliBean;

public class XmlCliCmdParser {
    
    private static final Logger logger = Logger.getLogger(XmlCliCmdParser.class);

    private InputStream in;
    
    public XmlCliCmdParser (InputStream in) {
        this.in = in;
    }

    public InputStream getIn() {
        return in;
    }

    public void setIn(InputStream in) {
        this.in = in;
    }
    
    public List<CliBean> getXmlCliCmdList() {
        List<CliBean> cliBeanList = new ArrayList<CliBean>();
        StringBuffer sb = new StringBuffer();
        BufferedReader br;
        CliBean cliBean = new CliBean();
        String line;
        
        br = new BufferedReader(new InputStreamReader(in));
        boolean startFlag = false;
        try {
            while ((line = br.readLine()) != null) {
                if (Pattern.matches("router>.*xml",line.trim().toLowerCase())) {
                    cliBean = new CliBean();
                    cliBean.setCmd(line);
                }
                if ("<COMMAND version=\"1.0\">".equals(line)) {
                    sb = new StringBuffer();
                    startFlag = true;
                }
                if ("</COMMAND>".equals(line)) {
                    sb.append(line);
                    cliBean.setResult(sb.toString());
                    cliBeanList.add(cliBean);
                    startFlag = false;
                }
                if (startFlag) {
                    sb.append(line);
                }
            }
        } catch (IOException e) {
            logger.error("Error when reading XmlCliCommand source");
            e.printStackTrace();
        }
        
        
        return cliBeanList;
    }
    
    public static void main(String argv[]) {
        File file = new File("./metadata/result.txt");
        try {
            XmlCliCmdParser parser = new XmlCliCmdParser(new FileInputStream(file));
            List<CliBean> list = parser.getXmlCliCmdList();
            
            for (CliBean bean : list) {
                System.out.println(bean.getCmd());
                OutputFormat format = OutputFormat.createPrettyPrint();
                XMLWriter writer = new XMLWriter( System.out, format );        
                writer.write(DocumentHelper.parseText(bean.getResult()));      
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
